package af.fx.control.dialogpane;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

/**
 * 对话框
 *
 * @author AF
 * @date 2024/1/27 18:59
 */
public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Button button = new Button("点击显示对话框");


        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #00ffff");
        anchorPane.getChildren().add(button);
        AnchorPane.setTopAnchor(button, 100.0);
        AnchorPane.setLeftAnchor(button, 100.0);

        button.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                DialogPane dialogPane = new DialogPane();
                dialogPane.setHeaderText("头部文字");
                dialogPane.setContentText("内容文字");

                dialogPane.getButtonTypes().add(0, ButtonType.OK);
                dialogPane.getButtonTypes().add(1, ButtonType.CANCEL);

                Button buttonOk = (Button) dialogPane.lookupButton(ButtonType.OK);
                Button buttonCancel = (Button) dialogPane.lookupButton(ButtonType.CANCEL);
                // 设置图片
//                ImageView imageView = new ImageView("file:\\D:\\code\\java\\javafx\\javafx-study\\icon.png");
//                dialogPane.setGraphic(imageView);
                // 显示扩展内容
                dialogPane.setExpandableContent(new Text("扩展内容"));
                dialogPane.setExpanded(false);


                Stage stage = new Stage();
                stage.setTitle("对话框");
                stage.setScene(new Scene(dialogPane));
                stage.initStyle(StageStyle.UTILITY);
                stage.initOwner(primaryStage);
                stage.initModality(Modality.WINDOW_MODAL);
                stage.setResizable(false);
                stage.show();

                buttonOk.setOnMouseClicked(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        stage.close();
                    }
                });
                buttonCancel.setOnMouseClicked(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        stage.close();
                    }
                });

                MyScheduledService myScheduledService = new MyScheduledService(dialogPane, stage);
                myScheduledService.setDelay(Duration.seconds(1.0)); // 延迟运行
                myScheduledService.setPeriod(Duration.seconds(1.0)); // 间隔多久运行一次
                myScheduledService.start();
            }
        });


        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("有期故事");
        primaryStage.setHeight(900);
        primaryStage.setWidth(900);
        primaryStage.show();
    }
}

class MyScheduledService extends ScheduledService<Integer> {
    private DialogPane dialogPane = null;
    private Stage stage = null;
    int i = 0;
    public MyScheduledService(DialogPane di, Stage stage) {
        this.dialogPane = di;
        this.stage = stage;
    }
    @Override
    protected Task<Integer> createTask() {
        return new Task<Integer>() {
            @Override
            protected Integer call() throws Exception {
                // 并不是UI线程，组件不能在这里更新
                System.out.println("call=" + Thread.currentThread().getName());

                return i = i + 1;
            }

            @Override
            protected void updateValue(Integer value) {
                // 这里是UI线程
                System.out.println("updateValue" + Thread.currentThread().getName());

                System.out.println("updateValue=" + value);

                if (value < 10) {
                    dialogPane.setContentText(value.toString());
                } else {
                    stage.close();
                    MyScheduledService.this.cancel(); // 内部类，调用父类MyScheduledService的取消
                }
            }
        };
    }
}
